import {EventEmitter, Injectable} from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import {BackCode} from "../model/backCode";
import {MaintainRecord} from "../model/maintainRecord";
import MTSConstants from "../config/config";

@Injectable()
export class MaintainService {

  servicename:string = "MaintainService";
  // listFreshEvent: EventEmitter<string> = new EventEmitter();   //列表刷新标志
  getRecordEvent: EventEmitter<string[]> = new EventEmitter();   //修改和查看标志(传入类型flag,维护记录oid)单独传入list则刷新列表

  constructor(private http: Http) { }

  //新增维护记录
  createMaintainRecord(postvalue:MaintainRecord): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/record`;
    postvalue.starttime = new Date(postvalue.starttimeToString);
    postvalue.endtime = new Date(postvalue.endtimeToString);
    const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('createMaintainRecord',error);});
  }

  //修改维护记录
  updateMaintainRecord(postvalue:MaintainRecord): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/record`;
    //let body = JSON.stringify(val);//这个也可以
    postvalue.starttime = new Date(postvalue.starttimeToString);
    postvalue.endtime = new Date(postvalue.endtimeToString);
    const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.put(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('updateMaintainRecord',error);});
  }

  //结束维护记录
  endMaintainRecord(oid:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/endrecord/${oid}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('endMaintainRecord',error);});
  }

  //获取维护记录列表
  getMaintainRecordList(): Promise<MaintainRecord[]>{
    const url = `${MTSConstants.HOSTUser}maintain/records`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getMaintainRecordList',error);});
  }

  //根据oid获取维护记录
  getMaintainRecordByOid(oid:string){
    const url = `${MTSConstants.HOSTUser}maintain/record/${oid}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getMaintainRecordByOId',error);});
  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }

  //查询维护记录分页信息
  getMaintainRecords(pageNumber:number,pageSize:number,vehicleoid:string,starttime:string,endtime:string,typeoid:string,status:number){
    const url = `${MTSConstants.HOSTUser}maintain/records/?pageNumber=${pageNumber}&pageSize=${pageSize}&vehicleoid=${vehicleoid}&startTime=${starttime}&endTime=${endtime}&typeoid=${typeoid}&status=${status}`;
    // console.log(url);
    return this.http.get(url)
      .toPromise()
      .then(res=>{return res.json()})
      .catch((error: any) => {return this.handleError('getMaintainRecords',error);});
  }

  //批量结束维护 传入oids以;分隔
  endMaintainRecords(oids:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/endrecords/?oids=${oids}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('endMaintainRecords',error);});
  }

}
